define(function (){
    
    // DOIT: 2016-12-23 发动机功率图
    
    //发动机数据分析-发动机功率图
    var enginePower=function(params,initChart,chartWithoutData,$dataChart){
        initChart();
        $.ajaxExt({
            url : "/engineManage/getEngineRunStatusListForGraph.htm",
            data : params
        }).done(function(result){
            if(result){
                var engiRtteSpdArr = [];
                var num1;
                var num2;
                var yaArr = [];
                var ybArr = [];
                $(result).each(function(i,item) {
                    engiRtteSpdArr.push(item.engiRtteSpd);
                    var engiRtteSpd = 0;
                    if(item.engiRtteSpd)
                        engiRtteSpd = item.engiRtteSpd;
                    var maxRefrTorq = 0;
                    if(item.maxRefrTorq)
                        maxRefrTorq = item.maxRefrTorq;
                    num1 = accMul(engiRtteSpd, maxRefrTorq);
                    num2 = num1/9550;
                    yaArr.push([item.engiRtteSpd , item.maxRefrTorq]);
                    ybArr.push([item.engiRtteSpd , num2.toFixed(2)-0]);
                })
                $dataChart.highcharts({
                    title: {
                        text: ''
                    },
                    xAxis: {
                        labels: {
                            align: 'center',
                            rotation: -45,
                            y: 50
                        },
                        title: {
                            useHTML:true,
                            text: '转速'
                        },
                        lineWidth: 1,
                        lineColor: '#808080'
                    },
                    credits: {
                        enabled: false
                    },
                    yAxis: [{
                        title: {
                            text: '扭矩(N.M)'
                        }
                    },{
                        min:0,
                        title: {
                            useHTML:true,
                            text: '功率(Kw)'
                        },
                        opposite: true
                    }],
                    tooltip: {
                        shared: true,
                        valueDecimals: 2
                    },
                    series: [
                        {
                            name: '扭矩',
                            type: 'spline',
                            color: '#74C4D4',
                            data:  yaArr
                        },
                        {
                            name: '功率',
                            type: 'spline',
                            color: '#9CCF31',
                            data: ybArr
                        }
                    ]
                });
            }
            else{
                chartWithoutData();
            }

        });
    };

    function createChart($dataChart,trData,peData,meData,rqxhlData,bqlData){
        weightData(trData, 550, 25, 5);
        weightData(peData, 50, 30, 4);
        weightData(meData, 700, 70, 3);
        weightData(rqxhlData, 10, 8, 2);
        function weightData(data, min, step, num) {//加权数据
            $.each(data, function (index, item) {
                var value = item[1];
                data[index][1] = (value - min) / step * 8 + (8 * (num - 1) * 8) + 180;
            });
        }

        function exrightData(data, min, step, num) {//除权数据
            var val = (data - 180 - (8 * (num - 1) * 8)) / 8 * step + min;
            return val;
        }

        var yTickPositions = [];
        for (var i = 0; i < 40; i++) {
            yTickPositions.push(180 + 8 * i);
        }
        $dataChart.highcharts({
            title: {
                text: ''
            },
            xAxis: {
                allowDecimals:true,
                startOnTick:true,
                endOnTick:true,
                title: {
                    text: 'n(r/min)'
                },
                showFirstLabel: true,
                lineWidth: 1,
                lineColor: '#808080',
                gridLineWidth: 1
            },
            credits: {
                enabled: false
            },
            yAxis: [
                {
                    min: 180,
                    max: 492,
                    title: {
                        text: ''
                    },
                    endOnTick: false,
                    tickPositions: yTickPositions,
                    gridLineWidth: 1,
                    labels: {
                        formatter: function () {
                            var num = (this.value - 180) / 8;
                            var ret = '';
                            if (num < 8) {
                                switch (num) {
                                    case 0:
                                        ret = '180';
                                        break;
                                    case 1:
                                        ret = '188';
                                        break;
                                    case 2:
                                        ret = '196';
                                        break;
                                    case 3:
                                        ret = '204';
                                        break;
                                    case 4:
                                        ret = '212';
                                        break;
                                    case 5:
                                        ret = '220';
                                        break;
                                    case 6:
                                        ret = '(g/kWh)';
                                        break;
                                    case 7:
                                        ret = '比气率';
                                        break;
                                }
                            }
                            if ((num < 24) && (num > 15)) {
                                switch (num) {
                                    case 16:
                                        ret = '700';
                                        break;
                                    case 17:
                                        ret = '770';
                                        break;
                                    case 18:
                                        ret = '840';
                                        break;
                                    case 19:
                                        ret = '910';
                                        break;
                                    case 20:
                                        ret = '980';
                                        break;
                                    case 21:
                                        ret = '1050';
                                        break;
                                    case 22:
                                        ret = '(Nm)';
                                        break;
                                    case 23:
                                        ret = 'Me';
                                        break;
                                }
                            }
                            if ((num < 40) && (num > 31)) {
                                switch (num) {
                                    case 32:
                                        ret = '550';
                                        break;
                                    case 33:
                                        ret = '575';
                                        break;
                                    case 34:
                                        ret = '600';
                                        break;
                                    case 35:
                                        ret = '625';
                                        break;
                                    case 36:
                                        ret = '650';
                                        break;
                                    case 37:
                                        ret = '675';
                                        break;
                                    case 38:
                                        ret = '(℃)';
                                        break;
                                    case 39:
                                        ret = 'Tr';
                                        break;
                                }
                            }
                            return ret;
                        }
                    }
                },
                {
                    min: 180,
                    max: 492,
                    //tickWidth: 1,
                    title: {
                        text: ''
                    },
                    endOnTick: false,
                    tickPositions: yTickPositions,
                    gridLineWidth: 0,
                    opposite: true,
                    labels: {
                        formatter: function () {
                            var num = (this.value - 180) / 8;
                            var ret = '';
                            if ((num < 16) && (num > 7)) {
                                switch (num) {
                                    case 8:
                                        ret = '10';
                                        break;
                                    case 9:
                                        ret = '18';
                                        break;
                                    case 10:
                                        ret = '26';
                                        break;
                                    case 11:
                                        ret = '34';
                                        break;
                                    case 12:
                                        ret = '42';
                                        break;
                                    case 13:
                                        ret = '50';
                                        break;
                                    case 14:
                                        ret = '(kg/h)';
                                        break;
                                    case 15:
                                        ret = '瞬时能耗';
                                        break;
                                }
                            }
                            if ((num < 32) && (num > 23)) {
                                switch (num) {
                                    case 24:
                                        ret = '50';
                                        break;
                                    case 25:
                                        ret = '80';
                                        break;
                                    case 26:
                                        ret = '110';
                                        break;
                                    case 27:
                                        ret = '140';
                                        break;
                                    case 28:
                                        ret = '170';
                                        break;
                                    case 29:
                                        ret = '200';
                                        break;
                                    case 30:
                                        ret = '(kw)';
                                        break;
                                    case 31:
                                        ret = 'Pe';
                                        break;
                                }
                            }
                            return ret;
                        }
                    }
                }
            ],
            tooltip: {
                shared: true,
                formatter: function () {
                    var html = '转速' + this.x + '<br>';
                    $.each(this.points, function (index, item) {
                        var val;
                        if (item.series.name == 'Tr') {
                            val = exrightData(item.y, 550, 25, 5);
                            html += 'Tr:' + val.toFixed(2) + ' ℃<br>';
                        }
                        if (item.series.name == '瞬时能耗') {
                            val = exrightData(item.y, 10, 8, 2);
                            html += '瞬时能耗:' + val.toFixed(2) + ' kg/h<br>';
                        }
                        if (item.series.name == '比气率') {
                            html += '比气率:' + item.y + ' g/kWh<br>';
                        }
                        if (item.series.name == 'Me') {
                            val = exrightData(item.y, 700, 70, 3);
                            html += 'Me:' + val.toFixed(2) + ' Nm<br>';
                        }
                        if (item.series.name == 'Pe') {
                            val = exrightData(item.y, 50, 30, 4);
                            html += 'Pe:' + val.toFixed(2) + ' kw<br>';
                        }
                    });
                    return html;
                },
                crosshairs: true
            },
            series: [
                {
                    name: '比气率',
                    type: 'spline',
                    marker: {
                        enabled: false
                    },
                    data: bqlData
                },
                {
                    name: '瞬时能耗',
                    type: 'spline',
                    marker: {
                        enabled: false
                    },
                    yAxis: 1,
                    data: rqxhlData
                },
                {
                    name: 'Me',
                    enableMouseTracking: true,
                    type: 'spline',
                    marker: {
                        enabled: false
                    },
                    data: meData
                },
                {
                    name: 'Pe',
                    enableMouseTracking: true,
                    type: 'spline',
                    marker: {
                        enabled: false
                    },
                    data: peData
                },
                {
                    name: 'Tr',
                    type: 'spline',
                    marker: {
                        enabled: false
                    },
                    data: trData
                }
            ]
        });
    };

    //乘法
    function accMul(arg1,arg2){
        var m=0,s1=arg1.toString(),s2=arg2.toString();
        try{m+=s1.split(".")[1].length}catch(e){}
        try{m+=s2.split(".")[1].length}catch(e){}
        return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
    };
    
    // DOIT: 2016-12-23 外特性曲线图

    var engineGraph=function(params,initChart,chartWithoutData,$dataChart){
        initChart();
        $.ajaxExt({
            url :"/engineManage/getEngineRunStatusListForGraph.htm",
            data : params
        }).done(function(result){
            if(result){
                var num1;
                var num2;
                var thanGas;
                var yaArr = [];
                var ybArr = [];
                var ycArr = [];
                var ydArr = [];
                var yeArr = [];
                $(result).each(function(i,item) {
                    //进气温度
                    yaArr.push([item.engiRtteSpd , item.engiAirAdmissionTept]);
                    //功率
                    var engiRtteSpd = 0;
                    var maxRefrTorq = 0;
                    if(item.engiRtteSpd)
                        engiRtteSpd = item.engiRtteSpd;
                    if(item.maxRefrTorq)
                        maxRefrTorq = item.maxRefrTorq;
                    num1 = accMul(engiRtteSpd, maxRefrTorq);
                    num2 = num1/9550;
                    ybArr.push([item.engiRtteSpd,num2.toFixed(2)-0]);
                    //最大参考扭矩
                    ycArr.push([item.engiRtteSpd , item.maxRefrTorq]);
                    //瞬时能耗
                    ydArr.push([item.engiRtteSpd , item.instFuelCsut]);
                    //比气耗
                    thanGas = item.instFuelCsut*1000/num2
                    yeArr.push([item.engiRtteSpd ,thanGas.toFixed(2)-0]);
                })
                createChart($dataChart,yaArr, ybArr, ycArr, ydArr, yeArr);
            }
            else{
                chartWithoutData();
            }
        });
    };
    return {
        enginePower: enginePower,
        engineGraph:engineGraph
    };
});